package commom

import (
	"strings"
	"time"
)

//时间戳转换时间日期,int64 format yyyy-mm-dd hh:ii:ss
func GetDate(timestamp int64, format string) string {
	format = strings.Replace(format, "yyyy", "2006", 0)
	format = strings.Replace(format, "y", "2006", 0)
	format = strings.Replace(format, "mm", "01", 0)
	format = strings.Replace(format, "dd", "02", 0)
	format = strings.Replace(format, "hh", "15", 0)
	format = strings.Replace(format, "ii", "04", 0)
	format = strings.Replace(format, "ss", "05", 0)

	timeobj := time.Unix(int64(timestamp), 0)
	date := timeobj.Format(format)
	return date
}

//得到当前时间
func GetCDate(format string) string {
	//formatstr := "2006-01-02 15:04:05"
	format = strings.Replace(format, "yyyy", "2006", 0)
	format = strings.Replace(format, "y", "2006", 0)
	format = strings.Replace(format, "mm", "01", 0)
	format = strings.Replace(format, "dd", "02", 0)
	format = strings.Replace(format, "hh", "15", 0)
	format = strings.Replace(format, "ii", "04", 0)
	format = strings.Replace(format, "ss", "05", 0)

	logTime := time.Unix(time.Now().Unix(), 0).Format(format)
	return logTime
}

//比较时间差
func GetDiff(start, end string) int64 {
	s := GetTimestamp(start)
	e := GetTimestamp(end)
	return e - s
}

//时间日期转换为时间戳 2020-10-12 14:19:53
func GetTimestamp(str string) int64 {
	//str := "2020-10-12 14:19:53"
	tmp := "2006-01-02 15:04:05"
	res, _ := time.ParseInLocation(tmp, str, time.Local)
	return res.Unix()
}
